<?php

	$timeout = "1";
	$self = $_SERVER['SERVER_ADDR'];

	$test_APP = $discovered_APP;

	if ( $test_APP == 'Joomla' ) {
		$application_URL = 'joomla.org';

		} else if ( $test_APP == 'Mambo' ) {
			$application_URL = 'mambo-foundation.org';

			} else if ( $test_APP == 'drupal' ) {
				$application_URL = 'drupal.org';

				} else if ( $test_APP == 'WordPress' ) {
					$application_URL = 'wordpress.org';

					} else if ( $test_APP == 'MODx' ) {
						$application_URL = 'modxcms.com';

						} else if ( $test_APP == 'e107' ) {
							$application_URL = 'e107.org';

							} else {
								$test_APP = 'openVISION';
								$application_URL = 'openvision.net.au';				
								}


		//set service checks
		$port[1] = "80";		$service[1] = "HTTP";					$address[1] = $self;
		$port[2] = "443";		$service[2] = "HTTPS";					$address[2] = $self;
		$port[3] = "21";		$service[3] = "FTP";					$address[3] = $self;
		$port[4] = "22";		$service[4] = "SSH/sFTP";				$address[4] = $self;
		$port[5] = "25";		$service[5] = "SMTP";					$address[5] = $self;
		$port[6] = "110";		$service[6] = "POP3";					$address[6] = $self;
		$port[7] = "143";		$service[7] = "IMAP";					$address[7] = $self;
		$port[8] = "3306";		$service[8] = "MYSQL";					$address[8] = $self;
		$port[9] = "80";		$service[9] = $test_APP .' Site';		$address[9] = $application_URL;

		//count arrays
		$ports = count($port);
		$ports = $ports + 1;
		$count = 1;

			$data .= "<table border='0' width='95%' cellspacing='0' cellpadding='0'>";

				while($count < $ports){

					if($address[$count]==""){ $address[$count] = ""; }

			        $fp = @fsockopen("$address[$count]", $port[$count], $errno, $errstr, $timeout);

						// On & Offline Services Shown
        				if (!$fp) {

							$data .= "<tr><td class='right-col' align='center' width='16px'><img src='css/images/status_no.png' width='16px' height='16px'></td><td class='cell_data'>&nbsp;&nbsp;". $service[$count] ."</td></tr>";
							} else {
								$data .= "<tr><td class='right-col' align='center' width='16px'><img src='css/images/status_yes.png' width='16px' height='16px'><td class='cell_data'>&nbsp;&nbsp;". $service[$count] ."</td></td></tr>";


// Only Online Services Shown...
//        				if ($fp) {
//								$data .= "<tr><td align='center' width='25px'><img src='images/uparrow.png'></td><td class='data-container'>". $service[$count] ."</td></tr>";

            				fclose($fp);
						}

				$count++;
				}

			$data .= "</table>";

			echo $data;

		echo "<br />";

		

// START Potentiall BAD Ports
/**
		$badPort[1] = "80";	$badService[1] = "IRC on 6666";	$badAddress[1] =$self;
		$badPort[2] = "6667";	$badService[2] = "IRC on 6667";	$badAddress[2] =$self;

		//count arrays
		$badPorts = count($badPort);
		$badPorts = $badPorts + 1;
		$badCount = 1;

			$badData .= "<table border='1' width='95%' cellspacing='1' cellpadding='1'><tr><th colspan='2' align='left'><b>Un-Expected Services</b></th></tr>";

				while($badCount < $badPorts){

					if($badAddress[$badCount]==""){ $badAddress[$badCount] = ""; }

			        $badfp = @fsockopen("$badAddress[$badCount]", $badPort[$badCount], $errno, $errstr, $timeout);

/// On & Offline Services Shown
        				if (!$badfp) {
							$badData .= "<tr><td class='cell_data'>&nbsp;<img src='css/images/status_yes.png' width='16px' height='16px'>&nbsp;". $badService[$badCount] ."</td><td class='right_col' align='center' width='16px'><img src='css/images/status_no.png' width='16px' height='16px'></td></tr>";
							} else {
								$badData .= "<tr><td class='cell_data' style=''>&nbsp;<img src='css/images/symbol-stop.png' width='16px' height='16px''>&nbsp;". $badService[$badCount] ."</td><td class='right-col' align='center' width='15px'><img src='css/images/arrowup.png' width='16px' height='16px'></td></tr>";
								$badPortMsg = 1;

// Only Online Services Shown...

//        				if ($badfp) {
//							$badData .= "<tr><td class='data-container' style='border: 1px solid #F57900; border-left: 12px solid #F57900; background-color: #fffacd;'>&nbsp;<img src='css/images/symbol-stop.png' width='15px' height='15px'>&nbsp;". $badService[$badCount] ."</td><td class='right-col' align='center' width='15px'><img src='css/images/arrowup.png' width='15px' height='15px'></td></tr>";
//							$badPortMsg = 1;

 //           				fclose($badfp);

//						} else {

/// Show Only Offline Services
///        				if (!$badfp) {
///							$badData .= "<tr><td class='data-container'>&nbsp;<img src='images/tick.png'>&nbsp;". $badService[$badCount] ."</td><td class='right-col' align='center' width='15px'><img src='images/downarrow.png'></td></tr>";
///							} else {
///								$badData .= "<tr><td class='data-container' style='border: 1px solid #F57900; border-left: 12px solid #F57900; background-color: #fffacd;'>&nbsp;<img src='../includes/js/ThemeOffice/warning.png'>&nbsp;". $badService[$badCount] ."</td><td class='right-col' align='center' width='15px'><img src='images/uparrow-1.png'></td></tr>";
///								$badPortMsg = 1;

						}

				$badCount++;
				}
			if ($badPortMsg == "1") {
			 $badData .= "<tr><td colspan='2'><div>". $wm_badPorts ."</div></td></tr>";
			} else {
			 $badData .= "<tr><td colspan='2'><div>". $gm_badPorts ."</div></td></tr>";
			}
			$badData .= "</table>";

			echo $badData;
**/
// END Potential BAD Ports


			$data1 .= "<br /><table width='95%' border='0' cellspacing='0' cellpadding='0'>";

			//Get The Server uptime & loads
			if (function_exists( 'exec' )) { $loadresult = @exec('uptime'); }
				else if (function_exists( 'passthru' )) { $loadresult = @passthru('uptime'); }
					else { echo "<tr><td colspan='2' class='cell_data'>Unable to acquire statistics.</td></tr>"; }

//			preg_match("#averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$loadresult,$avgs);
			preg_match("/averages?: ([0-9\.]+)?[\s]+([0-9\.]+)?[\s]+([0-9\.]+)/",$loadresult, $avgs);

			$uptime = explode(' up ', $loadresult);
			$uptime = explode(',', $uptime[1]);
			$uptime = $uptime[0].', '.$uptime[1];


		// Don't Show for Windows Machines as they don't have the required functions....
		if ($isWin != 'WIN') {

			$data1 .= "<tr><th align='left' colspan='2' class='cell_head_titles'>Load Average</th></tr><tr><td class='cell_data' colspan='2'>&nbsp;Previous: <br />&nbsp;&nbsp;&nbsp;&nbsp;1 minute&nbsp;&nbsp;:&nbsp;". $avgs[1] ."<br />&nbsp;&nbsp;&nbsp;&nbsp;5 minutes:&nbsp;". $avgs[2] ."<br />&nbsp;&nbsp;15 minutes:&nbsp;". $avgs[3] ."</td></tr>\n";

			$data1 .= "<tr><th align='left' colspan='2' class='cell_head_titles'>UpTime</th></tr><tr><td class='cell_data' colspan='2'>&nbsp;Up: ". $uptime ."</td></tr>";

			} else {
				$data1 .= "<tr><th align='left' colspan='2' class='cell_head_titles'>Load Averages</th></tr><tr><td class='cell_data' colspan='2'>&nbsp;&nbsp;N/A for Windows</td></tr>\n";
		
				$data1 .= "<tr><th align='left' colspan='2' class='cell_head_titles'>UpTime</th></tr><tr><td class='cell_data' colspan='2'>&nbsp;&nbsp;N/A for Windows</td></tr>";
				
				} // End isWin

			echo $data1;

			echo "<tr><th colspan='2' align='left' class='cell_head_titles'>Capacity</th></tr>";
			echo "<tr><td class='cell_data'>Disk Space:</td><td class='right-col' width='32%'>";

			if (function_exists('disk_total_space')) {

				$diskusage = round((disk_total_space("."))/(1024*1024));
				echo round($diskusage/1024)." GB";
	
				} else {
					echo "Not Supported.";
			
					}

	echo "</td></tr>";

	echo "<tr><td class='cell_data'>Free Space:</td><td class='right-col' width='32%'>";

		if (function_exists('disk_free_space')) {
			$diskusage = round((disk_free_space("."))/(1024*1024));
			echo round($diskusage/1024)." GB";

			} else {
				echo "Not Supported.";

				}

	echo "</td></tr>";

	echo "<tr><td class='cell_data'>Used Memory:</td><td class='right-col' width='32%'>";

		if (function_exists('memory_get_usage') && function_exists('ini_get')) {

			$memusage=(memory_get_usage())/1024;
			printf (" %d kB", $memusage);

			} else {
				echo "Not Supported.";

				}

	echo "</td></tr>";

	echo "<tr><td class='cell_data'>Free Memory:</td><td class='right-col' width='32%'>";

		if (function_exists('memory_get_usage') && function_exists('ini_get')) {
			$memusage=(memory_get_usage())/1024;
			$memory=(ereg_replace("[A-Za-z]","",ini_get('memory_limit')))*1024;
			$freemem=$memory-$memusage;
			printf (" %d kB", $freemem);

			} else {
				echo "Not Supported.";

				}

	echo "</td></tr>";
	echo "</table>";

	
?>